package world.hezi.handlers;

import kotlin.coroutines.CoroutineContext;
import net.mamoe.mirai.event.EventHandler;
import net.mamoe.mirai.event.ListeningStatus;
import net.mamoe.mirai.event.SimpleListenerHost;
import net.mamoe.mirai.event.events.MessageEvent;
import net.mamoe.mirai.message.data.At;
import net.mamoe.mirai.message.data.MessageChain;
import net.mamoe.mirai.message.data.MessageChainBuilder;
import net.mamoe.mirai.message.data.QuoteReply;
import org.jetbrains.annotations.NotNull;
import world.hezi.bot.BeatsaverBot;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;


public class GroupEventHandlers extends SimpleListenerHost {
    Map<Long, Set<String>> map = BeatsaverBot.CONCERN;
    Set<String> set = BeatsaverBot.CHINA;
    @Override
    public void handleException(@NotNull CoroutineContext context, @NotNull Throwable exception){
    }
    @NotNull
    @EventHandler
    public ListeningStatus onMessage(@NotNull MessageEvent event) throws Exception {
        try {
            if (event.getSource().getSubject().getId()==941873202L||event.getSource().getSubject().getId()==740097301L){
                int type = 0;
                String message = event.getMessage().contentToString();
                message = message.replaceAll("！","!");
                boolean flag = true;
                if (message.indexOf("!订阅列表")==0) {
                    if (!map.containsKey(event.getSender().getId()) || map.get(event.getSender().getId()).size() == 0) {
                        MessageChain chain = new MessageChainBuilder()
                                .append(new QuoteReply(event.getMessage()))
                                .append(new At(event.getSender().getId()))
                                .append("没有订阅的人")
                                .build();
                        event.getSubject().sendMessage(chain);
                    } else {
                        MessageChainBuilder messageChainBuilder = new MessageChainBuilder();
                        messageChainBuilder
                                .append(new QuoteReply(event.getMessage()))
                                .append(new At(event.getSender().getId()))
                                .append("订阅列表:\r\n");
                        for (String s : map.get(event.getSender().getId())) {
                            messageChainBuilder.append(s).append("\r\n");
                        }
                        event.getSubject().sendMessage(messageChainBuilder.build());
                    }
                }else if (message.indexOf("!订阅")==0){
                    type = 1;
                    String name = message.substring(4).trim();
                    if (name.length()==0)
                        return ListeningStatus.LISTENING;
                    if (!map.containsKey(event.getSender().getId())){
                        Set<String> set = new HashSet<>();
                        set.add(name);
                        map.put(event.getSender().getId(),set);
                    }else {
                        Set<String> set = map.get(event.getSender().getId());
                        if (set.contains(name)){
                            flag = false;
                        }else {
                            set.add(name);
                        }
                    }
                    MessageChain chain = new MessageChainBuilder()
                            .append(new QuoteReply(event.getMessage()))
                            .append(new At(event.getSender().getId()))
                            .append(flag?"订阅成功！":"订阅失败，不能重复订阅！")
                            .build();
                    event.getSubject().sendMessage(chain);
                }else if (message.indexOf("!删除订阅")==0){
                    type = 1;
                    String name = message.substring(6).trim();
                    if (name.length()==0)
                        return ListeningStatus.LISTENING;
                    if (!map.containsKey(event.getSender().getId())){
                        flag = false;
                    }else {
                        Set<String> set = map.get(event.getSender().getId());
                        if (!set.contains(name)){
                            flag = false;
                        }else {
                            set.remove(name);
                        }
                    }
                    MessageChain chain = new MessageChainBuilder()
                            .append(new QuoteReply(event.getMessage()))
                            .append(new At(event.getSender().getId()))
                            .append(flag?"删除订阅成功！":"删除订阅失败，订阅不存在！")
                            .build();
                    event.getSubject().sendMessage(chain);
                }else if ((message.indexOf("!添加国区谱师")==0)&&event.getSubject().getId()==941873202L){
                    type = 2;
                    String name = message.substring(8).trim();
                    if (name.length()==0)
                        return ListeningStatus.LISTENING;
                    if (set.contains(name)){
                        flag = false;
                    }else {
                        set.add(name);
                    }
                    MessageChain chain = new MessageChainBuilder()
                            .append(new QuoteReply(event.getMessage()))
                            .append(new At(event.getSender().getId()))
                            .append(flag?"添加成功！":"添加失败，不能重复添加！")
                            .build();
                    event.getSubject().sendMessage(chain);
                }else if ((message.indexOf("!删除国区谱师")==0)&&event.getSubject().getId()==941873202L){
                    type = 2;
                    String name = message.substring(8).trim();
                    if (name.length()==0)
                        return ListeningStatus.LISTENING;
                    if (!set.contains(name)){
                        flag = false;
                    }else {
                        set.remove(name);
                    }
                    MessageChain chain = new MessageChainBuilder()
                            .append(new QuoteReply(event.getMessage()))
                            .append(new At(event.getSender().getId()))
                            .append(flag?"删除成功！":"删除失败，没这个人！")
                            .build();
                    event.getSubject().sendMessage(chain);
                }else if (message.indexOf("!国区谱师列表")==0){
                    MessageChainBuilder messages = new MessageChainBuilder();
                    messages.append(new QuoteReply(event.getMessage()))
                            .append(new At(event.getSender().getId()))
                            .append("国区谱师列表:\r\n");
                    for (String s : set) {
                        messages.append(s).append("\r\n");
                    }
                    MessageChain chain = messages.build();
                    event.getSubject().sendMessage(chain);
                }
                if (flag){
                    switch (type){
                        case 1:{
                            File file = new File(BeatsaverBot.CONFIG_PATH+"\\concern.ser");
                            if (!file.exists())
                                file.createNewFile();
                            FileOutputStream fileOutputStream = new FileOutputStream(file);
                            ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
                            objectOutputStream.writeObject(map);
                            objectOutputStream.close();
                            fileOutputStream.close();
                            break;
                        }
                        case 2:{
                            File file = new File(BeatsaverBot.CONFIG_PATH+"\\china.ser");
                            if (!file.exists())
                                file.createNewFile();
                            FileOutputStream fileOutputStream = new FileOutputStream(file);
                            ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
                            objectOutputStream.writeObject(set);
                            objectOutputStream.close();
                            fileOutputStream.close();
                            break;
                        }
                    }
                }
            }
            return ListeningStatus.LISTENING;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ListeningStatus.LISTENING;
    }
}